package liming.ai.code.java_maven;

import liming.ai.code.java_maven.chat.QueryAi;
import liming.ai.data.deepseek.ChatCompletionRequest;
import liming.ai.tool.ReInput;
import liming.tool.file.FileRW;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class Main {
    public static void main(String[] args) throws Exception {
        File file = new File("./config.json");
        QueryAi queryAi = new QueryAi(file);
//        while (true){
//            String query = getQuery();
//            if(query.isEmpty()||query.endsWith("exit"))
//                break;
//            ChatCompletionRequest newRequest = QueryAi.getNewRequest(query);
//            queryAi.handleContent(newRequest,queryAi.valueToJson(queryAi.chat(query, newRequest)));
//        }
        System.out.println("结束");
    }

    private static String getQuery() throws IOException {
        ReInput input = new ReInput();
        StringBuilder sb = new StringBuilder();
        System.out.println("支持多行输入，输入 \"s\" 发送，" +
                "输入\"exit\"退出，" +
                "输入 \"file:\"读取文件或文件夹，" +
                sb.toString().length());
        input.input(System.console()==null? FileRW.FileEncoding.UTF8: FileRW.FileEncoding.AUTO, str->{
            if(str.startsWith("file:")) {
                read(sb,new File(str.substring(5)));
                return true;
            }
            else {
                sb.append("\n").append(str);
                return !str.equals("exit") && !str.equals("s");
            }
        });
        if(sb.toString().intern().endsWith("exit"))
            return "";
        return sb.toString();
    }

    private static void read(StringBuilder sb,File file) throws IOException {
        if(file==null) {
            return;
        }
        if(file.isDirectory()){
            for(File f : Objects.requireNonNull(file.listFiles()))
                read(sb,f);
        }
        else if(file.isFile()){
            String string = FileRW.readTextFile(file).toString();
            sb.append("\n").append("文件内容：\n").append(string);
            System.out.println("file: "+file+"读取长度："+string.length());
        }
    }

}
